package com.lkxicy.wos.service.impl;

import com.lkxicy.wos.mapper.SalaryRewordMapper;
import com.lkxicy.wos.mapper.StaffMapper;
import com.lkxicy.wos.pojo.entity.SalaryPayment;
import com.lkxicy.wos.mapper.SalaryPaymentMapper;
import com.lkxicy.wos.pojo.entity.SalaryReword;
import com.lkxicy.wos.pojo.entity.Staff;
import com.lkxicy.wos.pojo.vo.SalaryRecord;
import com.lkxicy.wos.service.SalaryPaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lkxicy
 * @since 2020-12-02
 */
@Service
public class SalaryPaymentServiceImpl extends ServiceImpl<SalaryPaymentMapper, SalaryPayment> implements SalaryPaymentService {

    @Autowired
    private SalaryPaymentMapper salaryPaymentMapper;

    @Autowired
    private SalaryRewordMapper salaryRewordMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Override
    public List<SalaryRecord> searchAllSalaryRecord(String staffNumber){
        List<SalaryRecord> salaryRecords=new ArrayList<SalaryRecord>();
        for(SalaryRecord s:salaryPaymentMapper.findOneAllRecord(staffNumber)) {
            // 构造一个新的vo来放到页面上显示
            SalaryRecord salaryRecord=s;
            // 这里要把奖惩表中的属于该月的奖惩记录写到这个VO里面
            salaryRecord.setDetails(salaryRewordMapper.findDetails(staffNumber,s.getPayTime()));
            salaryRecords.add(salaryRecord);
        }
        return salaryRecords;
    }

    @Override
    public String addAllSalaryRecord(Date effectTime) {
        boolean flag=false;
        for(Staff staff:staffMapper.findAllStaff()){
            // 本月已经发过工资了，不再次发工资  如果需要发双倍工资这样的操作，请前往工资奖惩
            if(salaryPaymentMapper.findOneIsPaied(staff.getStaffNumber(),effectTime)!=null){
                flag=true;
                continue;
            }
            SalaryPayment salaryPayment = new SalaryPayment();
            salaryPayment.setStaffNumber(staff.getStaffNumber());
            salaryPayment.setPayTime(effectTime);
            salaryPayment.setNormalSalary(staff.getSalary());

            // 这里可以优化   把for循环select 变成 selectBatch
            Double salsary=staff.getSalary();
            for(Double num:salaryRewordMapper.findOneSalaryReword(staff.getStaffNumber(),effectTime)){
                salsary+=num;
            }
            salaryPayment.setActualSalary(salsary);

            salaryPaymentMapper.insert(salaryPayment);
        }
        if(!flag){
            return "操作成功！";
        }
        else{
            return "操作成功！本月已经发过工资的员工在本次操作中不生效。";
        }

    }

    @Override
    public String addSalaryRecord(String staffNumber,Date effectTime) {
        if(salaryPaymentMapper.findOneIsPaied(staffNumber, effectTime)!=null){
            return "该员工本月已经发过工资了！";
        }
        Staff staff=staffMapper.findStaffByNumber(staffNumber);
        SalaryPayment salaryPayment = new SalaryPayment();
        salaryPayment.setStaffNumber(staff.getStaffNumber());
        salaryPayment.setPayTime(effectTime);
        salaryPayment.setNormalSalary(staff.getSalary());

        // 这里可以优化   把for循环select 变成 selectBatch
        Double salsary=staff.getSalary();
        for(Double num:salaryRewordMapper.findOneSalaryReword(staff.getStaffNumber(),effectTime)){
            salsary+=num;
        }
        salaryPayment.setActualSalary(salsary);

        salaryPaymentMapper.insert(salaryPayment);
        return "操作成功！";
    }
}
